<?php 
namespace haibao\cms\business;

use \haibao\cms\data\Calendar as CalendarData;
use haibao\frame\data\query\Condition;

class Calendar extends \haibao\cms\business\Base
{
	public static function getCalendarAtt($parentId)
	{
		$calendarData = new CalendarData();
		return $calendarData->getCalendarAttr($parentId);
	}

	public static function getModelById($id)
	{
		$calendarData = new CalendarData();
		return $calendarData->getOneById($id);
	}

	public static function getCalendarId($articleId)
	{
		$calendarData = new CalendarData();
		return $calendarData->getCalendarId($articleId);
	}

	public static function addModel($model)
	{
		$calendarData = new CalendarData();
		$calendarData->addCalendar($model);
		self::saveOperateHistory(self::DATA_ERROR_ARTICLE_CALENDAR_ADD, $model->toArray(false));
	}

	public static function updateModel($model)
	{
		$calendarData = new CalendarData();
		$calendarData->updateCalendar($model);
		self::saveOperateHistory(self::DATA_ERROR_ARTICLE_CALENDAR_UPDATE, $model->toArray(false));
	}

	public static function getImageUrl($imageId)
	{
		$calendarData = new CalendarData();
		return $calendarData->getImageUrl($imageId);
	}

	public static function getAllByFilter($filter)
	{
		$data = new CalendarData();
		return $data->getOne($filter);
	}

	/**
	 * 添加造型日历分类
	 */
	public static function addCalendarAtt($model)
	{
		self::checkOperation(self::DATA_ERROR_ARTICLE_CALENDAR_ADD);
		self::checkForm($model);
		$data = new CalendarData();
		return $data->addCalendarAtt($model);
		self::saveOperateHistory(self::DATA_ERROR_ARTICLE_CALENDAR_ADD, $model->toArray(false));
	}

	/**
	 * 删除造型日历分类
	 */
	public static function delCalendarAtt($id)
	{
		self::checkOperation(self::DATA_ERROR_ARTICLE_CALENDAR_DELETE);
		$data = new CalendarData();
		$data->delCalendarAtt($id);
		self::saveOperateHistory(self::DATA_ERROR_ARTICLE_CALENDAR_DELETE, $id);
	}

	/**
	 * 修改类别
	 */
	public static function updateCalendarAtt($model)
	{
		self::checkOperation(self::DATA_ERROR_ARTICLE_CALENDAR_UPDATE);
		self::checkFormEdit($model);
		$data = new CalendarData();
		$data->updateCalendarAtt($model);
		self::saveOperateHistory(self::DATA_ERROR_ARTICLE_CALENDAR_UPDATE, $model->toArray(false));
	}


	/**
	 * 根据id获取类别
	 */
	public static function getOneCalendarAttr($id)
	{
		$data = new CalendarData();
		return $data->getOneCalendarAttr($id);
	}

	private static function checkForm($model)
	{
		if (!$model->Name) {
			throw new BusinessException('类别不能为空');
		}

		$data = new CalendarData();
		$result = $data->getCalendarAttByName($model->Name, $model->ParentId);
		if ($result) {
			throw new BusinessException('类别不能重复');
		}
	}

	private static function checkFormEdit($model)
	{
		if (!$model->Name) {
			throw new BusinessException('类别不能为空');
		}

		$data = new CalendarData();
		$result = $data->getCalendarAttByEditName($model->Name, $model->Id, $model->ParentId);
		if ($result) {
			throw new BusinessException('类别不能重复');
		}
	}

	/**
	 * 获取造型日历首页的资讯列表
	 */
	public static function getCalendarIndex()
	{
		$data = new CalendarData();

		return $data->getCalendarIndex();
	}

	/**
	 * @param $calendarIds
	 * @return array
	 * 获取造型日历的造型单品
	 */
	public static function getCalendarProductsByCalendarIds($calendarIds)
	{
		$data = new CalendarData();

		return $data->getCalendarProduct($calendarIds);
	}

	/**
	 * @param $calendarId
	 * 获取造型日历首页推荐单品
	 */
	public static function getRecommendProductsByCalendarId($calendarId)
	{
		$data = new CalendarData();
		$products = $productIds = array();
		$products = $data->getRecommendProductsByCalendarId($calendarId);
		if ($products) {
			foreach ($products as $item) {
				$productIds[] = $item['ProductId'];
			}
			$productData = new \haibao\cms\data\Product();
			$getProductBrand = $productData->getBrandInfoByProductIds($productIds);
			if ($getProductBrand) {
				$relBrandTagIds = array();
				foreach ($getProductBrand as $data) {
					$relBrandTagIds[$data['BrandId']] = $data['TagId'];
				}
				$relBrandTopicIds = \haibao\cms\business\Tag::getTopicIdByTagId($relBrandTagIds);
				foreach ($products as $key => $data) {
					$products[$key]['BrandName'] = '';
					$products[$key]['BrandUrl'] = '';
					if (isset($getProductBrand[$data['ProductId']]['Name'])) {
						$products[$key]['BrandName'] = $getProductBrand[$data['ProductId']]['Name'];
						if (isset($relBrandTopicIds[$getProductBrand[$data['ProductId']]['TagId']])) {
							$products[$key]['BrandUrl'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BRANDS)
								. '/brand/' . $relBrandTopicIds[$getProductBrand[$data['ProductId']]['TagId']] . '/';
						}

					}
				}
			}
		}
		return $products;
	}

	public static function getProductsForCalendarArticle($calendarId)
	{
		$products = array();
		$data = new CalendarData();
		$products = $data->getProductsForCalendarArticle($calendarId);
		if ($products) {
			$productIds = array();
			foreach($products as $data){
				$productIds[] = $data['ProductId'];
			}
			$productData = new \haibao\cms\data\Product();
			$getProductBrand = $productData->getBrandInfoByProductIds(array_unique($productIds));

			if ($getProductBrand) {
				$relBrandTagIds = array();
				foreach ($getProductBrand as $data) {
					$relBrandTagIds[$data['BrandId']] = $data['TagId'];
				}
				$relBrandTopicIds = \haibao\cms\business\Tag::getTopicIdByTagId($relBrandTagIds);
				foreach ($products as $key => $data) {
					$brandKey = $data['ProductId'];
					if (isset($getProductBrand[$brandKey])) {
						$products[$key]['BrandNameCn'] = $getProductBrand[$brandKey]['NameCn'] ? $getProductBrand[$brandKey]['NameCn'] :
							$getProductBrand[$brandKey]['NameEn'];
						$products[$key]['BrandNameEn'] = $getProductBrand[$brandKey]['NameEn'];
						$products[$key]['BrandId'] = $getProductBrand[$brandKey]['BrandId'];
						$products[$key]['BrandImg'] = $getProductBrand[$brandKey]['ImgId'];
						$products[$key]['BrandStatus'] = $getProductBrand[$brandKey]['Status'];
						$products[$key]['BrandUrl'] = '';
						if (isset($relBrandTopicIds[$getProductBrand[$brandKey]['TagId']])) {
							$products[$key]['BrandUrl'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BRANDS)
								. '/brand/' . $relBrandTopicIds[$getProductBrand[$brandKey]['TagId']] . '/';
						}
					}
				}
			}
		}
		return $products;
	}

	/**
	 * 获取全部的造型日历
	 */
	public static function getAllCalendarAttr()
	{
		$data = new CalendarData();
		return $data->getAllCalendarAttr();
	}

	public static function getAttrListByAttrId($id, $page)
	{
		$attrs = array();
		$data = new CalendarData();
		$attrs = $data->getAttrListByAttrId($id, $page);
		return $attrs;
	}

	public static function getResultCountForCalendarList($attrId = 0)
	{
		$data = new CalendarData();
		return $data->getResultCountForCalendarList($attrId);
	}

	public static function getProductsByCalendarId($calendarId)
	{
		$data = new CalendarData();
		return $data->getProductsByCalendarId($calendarId);
	}

	public static function getHaibao()
	{
		$data = new CalendarData();
		return $data->getHaibao();
	}

	public static function getCalendarsInDate($year, $month)
	{
		$getCalendars = $articleIds = array();
		$data = new CalendarData();
		$getCalendars = $data->getCalendarsInDate($year, $month);
		if ($getCalendars) {
			foreach ($getCalendars as $key => $data) {
				$articleIds[] = $data['ArticleId'];
				$tmpArr[$data['ArticleId']] = $key;
				$getCalendars[$key]['PublishTime'] = $data['PublishTime']->format('Y-n-j');
				$getCalendars[$key]['Date'] = $data['PublishTime']->format('j');
				$getCalendars[$key]['ArticleUrl'] = \haibao\classlibrary\cms\Config::getConfig
					(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_CALENDAR) . '/article/' . $data['PublishId'] . '.htm';
			}
			$control = \haibao\cms\business\control\ArticleController::initArticleControl();
			$articleContent = new \haibao\cms\business\control\Base();
			$proImages = $control->getArticleProImage($articleIds, \haibao\cms\model\data\ArticleProImage::Level_SIX);
			$resArr = array();
			foreach ($proImages as $key => $image) {
				$resArr[$key] = $articleContent->getImageLink($image[0], 300, 400, 90, 0);
				$getCalendars[$tmpArr[$key]]['ProImage'] = $resArr[$key];
			}
		}
		return $getCalendars;


	}

	public static function checkLikeByCalendarIds($uid, $ids)
	{
		$resData = array();
		if (empty($uid) || empty($ids)) {
			throw new BusinessException('数据不能为空');
		}
		$data = new CalendarData();
		$resData = $data->checkLikeByCalendarIds($uid, $ids);
		return $resData;
	}

	public static function addLike($uid, $id)
	{
		$data = new CalendarData();
		$res = $data->addLike($uid, $id);
		return $res;
	}

	public static function getAttrsByArticleIds($articleIds) {
		$data = new CalendarData();
		$attrs = $data->getAttrsByArticleIds($articleIds);

		$attrInfos = $data->getAllCalendarAttr();
		$attrNames = array_values($attrInfos);
		$allAttr = $articleAttrs = array();
		for ($i = 0; $i < count($attrNames); $i++) {
			$allAttr += $attrNames[$i];
		}
		foreach ($attrs as $key => $attr) {
			if($attr){
				$articleAttrs[$key]['TagId'] = $attr;
				$articleAttrs[$key]['TagName'] = $allAttr[$attr];
				$articleAttrs[$key]['TagUrl'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_CALENDAR) . '/list/' . $attr . '/';

			}
		}
		return $articleAttrs;
	}

	public static function getCalendarAttsByCalendarId($calendarId)
	{
		$data = new CalendarData();
		return $data->getCalendarAttsByCalendarId($calendarId);
	}

	public static function getNewCalendarList(){
		$cache = new \haibao\cms\cache\Calendar();
		$calendarListCache = $cache->getCalendarNewList();
		if(!$calendarListCache){
			$filter = new \haibao\cms\model\filter\Article();
			$tagFilter = new \haibao\cms\model\filter\ArticleTag();
			$filter->select(array(
				$filter::CONDITION_FIELD_NAME_PUBLISHTIME,
				$filter::CONDITION_FIELD_NAME_ARTICLE_PUBLISHID,
				$filter::CONDITION_FIELD_NAME_TITLE,
				$filter::CONDITION_FIELD_NAME_ID
			));
			$filter->leftJoin($tagFilter, null, $filter::CONDITION_FIELD_NAME_ID, $tagFilter::CONDITION_FIELD_NAME_ARTICLE_ID, null);
			$tagFilter->where($tagFilter::CONDITION_FIELD_NAME_TAG_ID, Condition::CONDITION_EQUAL, \haibao\cms\model\data\ArticleTag::CALENDAR_TAG);
			$tagFilter->where($tagFilter::CONDITION_FIELD_NAME_IS_PRIMAIY, Condition::CONDITION_EQUAL, \haibao\cms\model\data\ArticleTag::PRIMAIY_IS);
			$filter->where($filter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\cms\model\data\Article::STATUS_ENABLE);
			$filter->where($filter::CONDITION_FIELD_NAME_PUBLISHED, Condition::CONDITION_EQUAL, \haibao\cms\model\data\Article::PUBLISHED_YES);
			$filter->order($filter::CONDITION_FIELD_NAME_PUBLISHTIME, false);
			$filter->limit(5);

			$articleIds = $calendarList = array();
			$week = array(
				1=>'星期一',
				2=>'星期二',
				3=>'星期三',
				4=>'星期四',
				5=>'星期五',
				6=>'星期六',
				7=>'星期天'
			);
			$articles = \haibao\cms\business\Article::getArticleList($filter);
			foreach ($articles as $article) {
				$articleIds[] = $article->Id;
				$calendarList[$article->Id]['Title'] = $article->Title;
				$calendarList[$article->Id]['Month'] = $article->PublishTime->format('M');
				$calendarList[$article->Id]['Day'] = $article->PublishTime->format('d');
				$calendarList[$article->Id]['Week'] = $week[$article->PublishTime->format('N')];
				$calendarList[$article->Id]['Url'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_CALENDAR) . '/article/' . $article->PublishId . '.htm';
			}
			$control = \haibao\cms\business\control\ArticleController::initArticleControl();
			$articleContent = new \haibao\cms\business\control\Base();
			if ($articleIds) {
				$proImages = $control->getArticleProImage($articleIds, \haibao\cms\model\data\ArticleProImage::Level_SIX);
				foreach ($proImages as $key => $image) {
					$imageUrl = $articleContent->getImageLink($image[0], 300, 400, 90, 0);
					$calendarList[$key]['ProImage'] = $imageUrl;
				}
				$cache->setCalendarNewList($calendarList);
			}
			return $calendarList;
		}else{
			return $calendarListCache;
		}


	}

}

?>